package com.tulele.vtool.service.impl;

import com.tulele.vtool.mapper.BDecorationArticlesMapper;
import com.tulele.vtool.mapper.BFloorPlanMapper;
import com.tulele.vtool.model.BDecorationArticles;
import com.tulele.vtool.model.BFloorPlan;
import com.tulele.vtool.service.BDecorationArticlesService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 文章作品详情表(BDecorationArticles)表服务实现类
 *
 * @author makejava
 * @since 2020-08-05 08:39:12
 */
@Service("bDecorationArticlesService")
public class BDecorationArticlesServiceImpl implements BDecorationArticlesService {
    @Resource
    private BDecorationArticlesMapper bDecorationArticlesMapper;

    @Resource
    private BFloorPlanMapper bFloorPlanMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public BDecorationArticles queryById(Integer id) {
        return this.bDecorationArticlesMapper.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<BDecorationArticles> queryAllByLimit(int offset, int limit) {
        return this.bDecorationArticlesMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 查询数据List
     *
     * @param bDecorationArticles 实例对象
     * @return 实例对象
     */
    @Override
    public List<BDecorationArticles> queryAllList(BDecorationArticles bDecorationArticles) {
        return this.bDecorationArticlesMapper.queryAllList(bDecorationArticles);
    }

    /**
     * 查询单条数据
     *
     * @param bDecorationArticles 实例对象
     * @return 实例对象
     */
    @Override
    public List<BDecorationArticles> queryOneById(BDecorationArticles bDecorationArticles) {
        return this.bDecorationArticlesMapper.queryOneById(bDecorationArticles);
    }

    /**
     * 新增数据
     *
     * @param bDecorationArticles 实例对象
     * @return 实例对象
     */
    @Override
    public BDecorationArticles insert(BDecorationArticles bDecorationArticles) {
        this.bDecorationArticlesMapper.insert(bDecorationArticles);
        return bDecorationArticles;
    }

    /**
     * 修改数据
     *
     * @param bDecorationArticles 实例对象
     * @return 实例对象
     */
    @Override
    public BDecorationArticles update(BDecorationArticles bDecorationArticles) {
        BDecorationArticles bDecorationCount = this.bDecorationArticlesMapper.queryById(bDecorationArticles.getId());
        bDecorationCount.setShareNum(bDecorationCount.getShareNum() + 1);

        this.bDecorationArticlesMapper.update(bDecorationCount);

        return this.queryById(bDecorationArticles.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.bDecorationArticlesMapper.deleteById(id) > 0;
    }

    /**
     * 添加数据
     *
     * @param bDecorationArticles 实例对象
     * @return 实例对象
     */
    @Override
    public void addBDecorationArticle(BDecorationArticles bDecorationArticles) {

        List<BFloorPlan> BFloorPlanInfo = bDecorationArticles.getBFloorPlans();

        this.bDecorationArticlesMapper.insert(bDecorationArticles);

        if (CollectionUtils.isNotEmpty(BFloorPlanInfo)) {
            BFloorPlanInfo.stream().forEach(info -> {
                BFloorPlan bFloorIds = new BFloorPlan();

                bFloorIds.setTypeId(info.getTypeId());
                bFloorIds.setTypeName(info.getTypeName());
                bFloorIds.setPicturePath(info.getPicturePath());
                bFloorIds.setDecorationId(bDecorationArticles.getId());
                bFloorIds.setUserId(bDecorationArticles.getUserId());
                bFloorIds.setCreateTime(new Date());
                bFloorIds.setUpdateTime(new Date());
                bFloorIds.setFloorFlag(1);

                this.bFloorPlanMapper.insert(bFloorIds);
            });
        }
    }

    @Override
    public List<BDecorationArticles> queryAllListBySpecialSize(Integer userId, int recordSize) {
        return bDecorationArticlesMapper.queryAllListBySpecialSize(userId, recordSize);
    }

    /**
     * 更新数据
     *
     * @param bDecorationArticles 实例对象
     * @return 实例对象
     */
    @Override
    public void updateOneByBDecoration(BDecorationArticles bDecorationArticles) {
        Integer intId = bDecorationArticles.getId();

        BDecorationArticles bDecorationUpdate = new BDecorationArticles();

        bDecorationUpdate.setId(intId);
        bDecorationUpdate.setUserId(bDecorationArticles.getUserId());
        bDecorationUpdate.setCoverPlan(bDecorationArticles.getCoverPlan());
        bDecorationUpdate.setHeadLine(bDecorationArticles.getHeadLine());
        bDecorationUpdate.setRoomNumber(bDecorationArticles.getRoomNumber());
        bDecorationUpdate.setWorksStyle(bDecorationArticles.getWorksStyle());
        bDecorationUpdate.setWorksArea(bDecorationArticles.getWorksArea());
        bDecorationUpdate.setWorksPrices(bDecorationArticles.getWorksPrices());
        bDecorationUpdate.setWorksIntroduce(bDecorationArticles.getWorksIntroduce());
        bDecorationUpdate.setUpdateTime(new Date());
        bDecorationUpdate.setWorksFlag(1);//（0停用 1正常）

        //更新装修文章
        this.bDecorationArticlesMapper.update(bDecorationUpdate);

        //查询装修文章图片
        List<BFloorPlan> bFloorPlanDel = this.bFloorPlanMapper.selectForDecorationId(intId);

        //删除装修文章图片
        if (CollectionUtils.isNotEmpty(bFloorPlanDel)) {
            bFloorPlanDel.stream().forEach(info -> {
                this.bFloorPlanMapper.deleteById(info.getId());
            });
        }

        //插入新的装修文章图片
        List<BFloorPlan> BFloorPlanInfo = bDecorationArticles.getBFloorPlans();
        if (CollectionUtils.isNotEmpty(BFloorPlanInfo)) {
            BFloorPlanInfo.stream().forEach(info -> {
                BFloorPlan bFloorIds = new BFloorPlan();

                bFloorIds.setTypeId(info.getTypeId());
                bFloorIds.setTypeName(info.getTypeName());
                bFloorIds.setPicturePath(info.getPicturePath());
                bFloorIds.setDecorationId(intId);
                bFloorIds.setUserId(bDecorationArticles.getUserId());
                bFloorIds.setCreateTime(new Date());
                bFloorIds.setUpdateTime(new Date());
                bFloorIds.setFloorFlag(1);

                this.bFloorPlanMapper.insert(bFloorIds);
            });
        }

    }
}
